package com.ttxs.uaa.api.common;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ttxs.common.core.valid.Create;
import com.ttxs.common.core.valid.Update;
import com.ttxs.common.core.vo.R;
import com.ttxs.common.core.constant.Api;
import com.ttxs.common.core.constant.Common;
import com.ttxs.uaa.entity.SysPlatform;
import com.ttxs.uaa.service.ISysPlatformService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author john.yi@qq.com
 * @since 2021-08-17
 */
@RestController
@RequestMapping(Api.Common.Platform.PREFIX)
@RequiredArgsConstructor
public class PlatformApi {
    private final ISysPlatformService sysPlatformService;

    @GetMapping
    public R list(@RequestParam("page") int pageNo,
                  @RequestParam("limit") int pageSize,
                  @RequestParam(value = "platformId", required = false) String platformId,
                  @RequestParam(value = "platformName", required = false) String platformName,
                  @RequestParam(value = "platformNameZh", required = false) String platformNameZh,
                  @RequestParam(value = "description", required = false) String description) {
        Page<SysPlatform> page = sysPlatformService.page(Page.of(pageNo, pageSize),
                new LambdaQueryWrapper<SysPlatform>()
                        .eq(StringUtils.hasText(platformId), SysPlatform::getPlatformId, platformId)
                        .like(StringUtils.hasText(platformName), SysPlatform::getPlatformName, platformName)
                        .like(StringUtils.hasText(platformNameZh), SysPlatform::getPlatformNameZh, platformNameZh)
                        .like(StringUtils.hasText(description), SysPlatform::getDescription, description));
        return R.ok(page);
    }

    @PostMapping
    public R add(@RequestBody @Validated(Create.class) SysPlatform sysPlatform) {
        // todo: secret在后续版本启用，当前任意设置
        sysPlatform.setPlatformSecret("111222");
        sysPlatformService.saveAndCreateManagerUser(sysPlatform);
        return R.ok(sysPlatform);
    }

    @GetMapping("/{id}")
    public R get(@PathVariable("id") String platformId) {
        return R.ok(sysPlatformService.getById(platformId));
    }

    @GetMapping(Api.Platform.SEARCH)
    public R search(SysPlatform sysPlatform) {
        return R.ok(sysPlatformService.list(new QueryWrapper<>(sysPlatform)));
    }

    @DeleteMapping("/{id}")
    public R del(@PathVariable("id") String platformId) {
        if(Common.Global.ADMIN_PLATFORM_ID.equals(platformId)) {
            return R.error(500,"管理平台不可以删除");
        }
        return R.ok(sysPlatformService.removeById(platformId));
    }

    @PutMapping("/{id}")
    public R update(@PathVariable("id") String platformId, @RequestBody @Validated(Update.class) SysPlatform sysPlatform) {
        if(Common.Global.ADMIN_PLATFORM_ID.equals(platformId)) {
            return R.error(500,"管理平台不可以修改");
        }
        sysPlatform.setPlatformId(platformId);
        return R.ok(sysPlatformService.saveOrUpdate(sysPlatform));
    }

}
